﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDTO;
using MesBSW.View.Dialog.Role;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MesBSW.View.RolePages
{
    public partial class UserInfoFrm : UIPage
    {

        #region "内部变量"
        private UserInfoControler ctrl = new UserInfoControler();
        private List<BaseUserDTO> userList = new List<BaseUserDTO>();
        #endregion

        public UserInfoFrm()
        {
            InitializeComponent();
            if (MainFrm.LoginUser.RoleModel != null && MainFrm.LoginUser.RoleModel.role_name.Equals("系统管理员"))
            {
                this.btnAddUser.Enabled = true;
            }
            else
            {
                this.btnAddUser.Enabled = false;
            }
        }

        #region "控件事件"

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            UserInfoDia tmpDia = new UserInfoDia();
            tmpDia.ShowDialog();

            this.btnSearch_Click(sender, e);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string userName = "";
            if ((MainFrm.LoginUser.RoleModel != null && MainFrm.LoginUser.RoleModel.role_name.Equals("系统管理员")) == false)
            {
                userName = MainFrm.LoginUser.user_name;
            }

            this.userList = this.ctrl.SearchUserByUserName(userName);
            if (this.userList != null && this.userList.Count > 0)
            {
                DataTable userDt = this.ListToDataTable(this.userList);
                this.dgvUser.DataSource = userDt;
            }
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 将用户信息由List转成DataTable
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private DataTable ListToDataTable(List<BaseUserDTO> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("user_name");
            dt.Columns.Add("pwd");
            dt.Columns.Add("real_name");
            dt.Columns.Add("role_name");
            dt.Columns.Add("base_dept_id");
            dt.Columns.Add("dept_name");
            dt.Columns.Add("sex");
            dt.Columns.Add("sex_name");
            dt.Columns.Add("email");
            dt.Columns.Add("phone");
            dt.Columns.Add("is_del");
            dt.Columns.Add("des");

            if (list != null)
            {
                foreach (var item in list)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = item.id;
                    dr["user_name"] = item.user_name;
                    dr["pwd"] = item.pwd;
                    dr["real_name"] = item.real_name;
                    dr["role_name"] = item.RoleModel.role_name;
                    dr["base_dept_id"] = item.base_dept_id;
                    dr["dept_name"] = item.deptModel.dept_name;
                    dr["sex"] = item.sex;
                    dr["sex_name"] = item.SexName;
                    dr["email"] = item.email;
                    dr["phone"] = item.phone;
                    dr["is_del"] = item.is_del;
                    dr["des"] = item.des;
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        #endregion

        private void dgvUser_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (this.dgvUser.Rows.Count > 0)
            {
                DataRow tmpRow = ((DataRowView)this.dgvUser.Rows[e.RowIndex].DataBoundItem).Row;
                int userID = int.Parse(tmpRow["id"].ToString());
                var query = this.userList.Where(u => u.id == userID);
                BaseUserDTO tmpUser = query.First();
                UserInfoDia tmpDia = new UserInfoDia(tmpUser);
                tmpDia.ShowDialog();

                this.btnSearch_Click(sender, e);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            userList = new List<BaseUserDTO>();
            DataTable dt = this.ListToDataTable(userList);
            this.dgvUser.DataSource = dt;
        }

        private void btnBarCode_Click(object sender, EventArgs e)
        {
            UIMessageBox.ShowWarning("该功能尚未实现！");
            //List<SiteInfo> printList = new List<SiteInfo>();
            //string basePath = Application.StartupPath + "\\员工二维码";
            //string fileName = "";

            //if (!System.IO.Directory.Exists(basePath))
            //{
            //    System.IO.Directory.CreateDirectory(basePath);
            //}

            //if (this.userList != null)
            //{
            //    try
            //    {
            //        foreach (var user in this.userList)
            //        {
            //            fileName = user.real_name + "(" + user.user_name + ").jpg";
            //            Bitmap tmpBmp = BarCodeHook.InitQCBarCode(user.user_name,user.real_name + "(" + user.user_name + ")", 300, 300);
            //            tmpBmp.Save(basePath + "\\" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            //        }
            //        UIMessageBox.ShowSuccess("二维码已经成功生成！");
            //        Process.Start(basePath);
            //    }
            //    catch (Exception ex)
            //    {
            //        UIMessageBox.ShowError("二维码生成失败！" + "\r\n" + ex.Message);
            //    }
            //}
            //else
            //{
            //    UIMessageBox.ShowWarning("没有任何二维码可以生成！");
            //}
        }
    }
}
